﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_TiDanTemplete : DockContent
    {


        private DGBY.BLL.TiDanTempleteTab m_bllTiDanTempleteTab = new DGBY.BLL.TiDanTempleteTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectPortID = 0;

        public Frm_TiDanTemplete()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            ultraGrid1.DataSource = null;
            m_ds = m_bllTiDanTempleteTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
         
            }
        }

        private void buttonShowAllRecordPort_Click_1(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        //private void dataGridViewPortList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        //{
        //    string strPortID = this.dataGridViewPortList.Rows[this.dataGridViewPortList.CurrentCell.RowIndex].Cells[0].Value.ToString();
        //    m_nSelectPortID = Convert.ToInt32(strPortID, 10);
        //    cellclick();
        //}

        private void cellclick()
        {
            DGBY.Model.TiDanTempleteTab modelTiDanTempleteTab = m_bllTiDanTempleteTab.GetModel(m_nSelectPortID);
            if (modelTiDanTempleteTab != null)
            {
                this.textBoxPort.Text = modelTiDanTempleteTab.TiDanTempleteName;
            }
            else
            {
                this.textBoxPort.Text = "";
            }
        }

        //private void dataGridViewPortList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    {
        //        using (SolidBrush b = new SolidBrush(dataGridViewPortList.RowHeadersDefaultCellStyle.ForeColor))
        //        {
        //            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        //        }
        //    }
        //}

        private void buttonAddPort_Click_1(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.TiDanTempleteTab modelTiDanTempleteTab = new DGBY.Model.TiDanTempleteTab();
                modelTiDanTempleteTab.TiDanTempleteName = this.textBoxPort.Text;
                modelTiDanTempleteTab.ReservField1 = "";

                int nResult = m_bllTiDanTempleteTab.Add(modelTiDanTempleteTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "添加", modelTiDanTempleteTab.TiDanTempleteName);

                    m_nSelectPortID = m_bllTiDanTempleteTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonModifyPort_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (m_bllTiDanTempleteTab.GetModel(m_nSelectPortID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.TiDanTempleteTab modelTiDanTempleteTab = m_bllTiDanTempleteTab.GetModel(m_nSelectPortID);
                modelTiDanTempleteTab.TiDanTempleteName = this.textBoxPort.Text;
                //modelTiDanTempleteTab.TiDanTempleteTabID = m_nSelectPortID;

                m_bllTiDanTempleteTab.Update(modelTiDanTempleteTab);

                Frm_Main.Log2DB(this.Text, "修改", modelTiDanTempleteTab.TiDanTempleteName);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonDelPort_Click(object sender, EventArgs e)
        {
            if (m_bllTiDanTempleteTab.GetModel(m_nSelectPortID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllTiDanTempleteTab.Delete(m_nSelectPortID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclick();
        }

        private void Frm_TiDanTemplete_Load(object sender, EventArgs e)
        {

        }

        private void Frm_TiDanTemplete_Load_1(object sender, EventArgs e)
        {
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelPort.Visible = true;
            }
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strBYCompID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectPortID = Convert.ToInt32(strBYCompID, 10);
            cellclick();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

      

      


    }
}
